#include "testtablewriter.h"
#include "ui_tablewidget.h"
#include "datatable.h"
#include "tablewriter.h"

#include <QApplication>
#include <QTableWidget>
#include <QTime>
#include <QDebug>
#include <QThreadPool>

static const int Rows = 100;
static const int Cols = 50;
TestTableWriter::TestTableWriter(QWidget *parent) : QWidget(parent)
{
    m_ui = new Ui::TableWidget;
    m_ui->setupUi(this);

    connect(m_ui->pushButton, SIGNAL(clicked()), this, SLOT(exportHtml()));
//    populateTable(m_ui->tableWidget);
//    populateTable(m_ui->tableWidget_2);
}

void TestTableWriter::exportHtml()
{
    TableData* td1 = new TableData(Rows, Cols);
    td1->setName("Table 1");
    populateTableData(td1, 100);

    QStringList horLabels;
    QStringList verLabels;
    for (int i = 0; i < Cols; ++i) {
        verLabels.append(QString("Ver Label %1").arg(i + 1));
        horLabels.append(QString("Hor Label %1").arg(i + 1));
    }
    td1->setLabels(Qt::Horizontal, horLabels);
    td1->setLabels(Qt::Vertical, verLabels);

    TableData* td2 = new TableData(Rows, Cols);
    populateTableData(td2, 1000);
    td2->setLabels(Qt::Horizontal, horLabels);
    td2->setName("Table 2");

    TableData* td3 = new TableData(Rows, Cols);
    populateTableData(td3, 10000);
    td3->setLabels(Qt::Horizontal, horLabels);
    td3->setName("Table 3");

    TableData* td4 = new TableData(Rows, Cols);
    populateTableData(td4, 432);
    td4->setLabels(Qt::Horizontal, horLabels);
    td4->setName("Table 4");

    TableData* td5 = new TableData(Rows, Cols);
    populateTableData(td5, 99);
    td5->setLabels(Qt::Horizontal, horLabels);
    td5->setName("Table 5");

    TableWriter* td = new TableWriter();
    td->setFileName("f:/1.html");
    td->addTableData(td1);
    td->addTableData(td2);
    td->addTableData(td3);
    td->addTableData(td4);
    td->addTableData(td5);
    QThreadPool::globalInstance()->start(td);
}

void TestTableWriter::populateTable(QTableWidget *table)
{
    table->setRowCount(Rows);
    table->setColumnCount(Cols);
    qsrand(QTime::currentTime().msec());
    for (int c = 0; c < Cols; ++c) {
        for (int r = 0; r < Rows; ++r) {
            auto item = new QTableWidgetItem;
            item->setText(QString::number(qrand() % 1000));
            table->setItem(r, c, item);
        }
    }
}

void TestTableWriter::populateTableData(TableData *td, int mod)
{
    qsrand(QTime::currentTime().msecsSinceStartOfDay());
    for (int c = 0; c < Cols; ++c) {
        for (int r = 0; r < Rows; ++r)
            td->setData(c, r, QString::number(qrand() % mod));
    }
}

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    TestTableWriter win;
    win.show();
    return app.exec();
}
